/**
 * User: kyleolivo
 * Date: 2/9/14
 * Time: 7:52 PM
 */
public class Diamond {  
    public static void main(String[] args) {
        Diamond myDiamond = new Diamond();
        myDiamond.printIsocelesTriangle(3);
        myDiamond.printDiamond(3);
        myDiamond.printDiamondWithName(3);
    }

    private void printDiamondWithName(int lineNumber) {
        System.out.print(createTriangle(lineNumber, true));
        System.out.println("Kyle");
        System.out.print(reversed(createTriangle(lineNumber, true)));
        System.out.println("---------------");
    }

    private void printDiamond(int lineNumber) {
        System.out.print(createTriangle(lineNumber, false));
        System.out.print(reversed(createTriangle(lineNumber, true)));
        System.out.println("---------------");
    }

    private String reversed(String text) {
        String reversedString = "";
        for (String line : text.split("\n")) {
            reversedString = line + "\n" + reversedString;
        }
        return reversedString;
    }

    private void printIsocelesTriangle(int lineNumber) {
        System.out.print(createTriangle(lineNumber, false));
        System.out.println("---------------");
    }

    private String createTriangle(int lineNumber, boolean removeLastLine) {
        String triangle = "";
        int largestLine = findNthOdd(lineNumber);
        if (removeLastLine) {
            lineNumber--;
        }
        for (int i = 1; i <= lineNumber; i++) {
            int asterisksInThisLine = findNthOdd(i);
            int paddingForThisLine = (largestLine - asterisksInThisLine) / 2;
            String asterisksString = buildString(asterisksInThisLine, "*");
            String paddingString = buildString(paddingForThisLine, " ");
            triangle += paddingString + asterisksString + paddingString + "\n";
        }
        return triangle;
    }

    private String buildString(int repeatNumber, String character) {
        String copiedString = "";
        for (int i = 0; i < repeatNumber; i++) {
            copiedString += character;
        }
        return copiedString;
    }

    private int findNthOdd(int odd) {
        int oddNumber = 1;
        for (int i = 1; i < odd; i++) {
            oddNumber += 2;
        }
        return oddNumber;
    }
}
